// pages/remark/remark.js
const HTTP = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isRemark:true,
        height: 0,
        iconList: [
            {
                src: HTTP.host + '/static/index/images/icon_tuijian.png',
                value: '强烈推荐',
                id: 1
            },
            {
                src: HTTP.host + '/static/index/images/icon_henmanyi.png',
                value: '很满意',
                id: 2
            },
            {
                src: HTTP.host + '/static/index/images/icon_haibucuo.png',
                value: '还不错',
                id: 3
            },
            {
                src: HTTP.host + '/static/index/images/icon_yiban.png',
                value: '一般',
                id: 4
            },
            {
                src: HTTP.host + '/static/index/images/icon_chaping.png',
                value: '差评',
                id: 5
            }
        ],
        fileList: [],
        order_id:'',
        mark: 0,
        content: '',
        go: false
    },
    getDelImg(e){
        console.log(e);
        this.deleteImg(e.detail.item);
    },
    isImgFirst(imgList){
        let length = this.data.fileList.length;
        if( length === 0){
            this.setData({
                fileList:imgList
            });
        }else if(0< length&&length<6){
            let arr = this.data.fileList;
            imgList.map(item=>{
                arr.push(item)
            });
            console.log(arr);
            this.setData({
                fileList:arr
            });
        }else if(length >= 6){
            wx.showToast({
                icon:'none',
                title:'最多上传6张图片哦~'
            });
            return false;
        }
        console.log(length);
    },
    postRemark(){
        HTTP.postData('/api/order/evaluate',{
            pic:this.data.fileList,
            content:this.data.content,
            order_id:this.data.order_id,
            status:this.data.mark
        },res=>{
            this.setData({
                isRemark:false
            });
        });
    },
    getOrderInfo(){
        HTTP.getData('/api/order/evaluatePage',{
            order_id:this.data.order_id
        },res=>{
            console.log(res);
            this.setData({
                create_time:res.data.create_time,
                id:res.data.id,
                payment:res.data.payment,
                shop_id:res.data.shop_id,
                shop_name:res.data.shop_name
            });
        });
    },
    goOrder(){
        wx.switchTab({
            url: '../order/order',
          });
    },
    deleteImg(e){
        wx.showModal({
            title: '提示',
            content: '确认删除当前图片',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#01D080',
            confirmText: '确定',
            confirmColor: '#01D080',
            success: (result) => {
                if (result.confirm) {
                    console.log(e);
                    let arr = this.data.fileList;
                    arr = arr.filter(item=>{
                        return item !== e;
                    });
                    console.log(arr)
                    this.setData({
                        fileList:arr
                    });
                }
            },
            fail: () => {},
            complete: () => {}
        });
          
    },
    setNumber() {
        this.setData({
            go: false
        });
    },
    checkNumber() {
        if (this.data.content.length < 15) {
            wx.showToast({
                icon: 'none',
                title: '请输入至少15个字哦~'
            });
            return false;
        } else {
            this.setData({
                go: true
            });
        }
    },
    inputChange(e) {
        this.setData({
            content: e.detail.value
        });
    },
    chooseMark(e) {
        let mark = Number(e.currentTarget.dataset.id);
        this.setData({
            mark: mark
        });
    },
    //发布
    release() {
        if (this.data.mark === 0) {
            wx.showToast({
                title: '请选择表情~',
                icon: 'none',
            });
            return false;
        } else if (this.data.content.length === 0) {
            wx.showToast({
                title: '请输入您的点评~',
                icon: 'none',
            });
            return false;
        } else {
            if (this.data.go) {
                // 发布！！
                console.log('ok')
                this.postRemark();
            }
        }

    },
    upPic() {
        wx.chooseImage({
            count:6,
            success:res=> {
                const tempFilePaths = res.tempFilePaths;
                let pathList = res.tempFilePaths;
                console.log(res);
                pathList.map(item=>{
                    this.handleUpPic(item);
                });
            }
        })
    },
    handleUpPic(item){
        wx.uploadFile({
            url: HTTP.host + '/api/order/uploads', //仅为示例，非真实的接口地址
            filePath: item,
            name: 'pic',
            header: {
                "Content-Type": "multipart/form-data",
            },
            formData: {
                'Authorization': wx.getStorageSync('token')
            },
            success:res=>{
                console.log('确实成功了');
                let data = JSON.parse(res.data);
                this.isImgFirst(data.data);
                console.log(this.data.fileList);
            },
            fail: res => {
            },
            complete: res => {
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(this.data.height);
        console.log(wx.getSystemInfoSync().windowHeight);
        console.log(wx.getSystemInfoSync().windowHeight-212);
        this.setData({
            order_id:Number(options.id),
            height:wx.getSystemInfoSync().windowHeight-212
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getOrderInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})